<?php
class Control_Gm_serverConfig {
    function serverConfig() {
        if($_FILES ){
            if($_FILES["file"]["error"] > 0){
                MooView::set('fileUploadCode', -1);
                MooView::set('fileUploadMessage', '文件上传失败');
            }else{
                $name = $_FILES["fileUpload"]["name"];
                $type = $_FILES["fileUpload"]["type"];
                $size_kb = $_FILES["fileUpload"]["size"] / 1024;
                $tmp_name = $_FILES["fileUpload"]["tmp_name"];
                // 文件类型验证
                $typeArr = array('xml');
                $node_type = $this->getFileExt($type);
                if(!in_array($node_type, $typeArr)) {
                    MooView::set('fileUploadCode', -1);
                    MooView::set('fileUploadMessage', '上传的文件类型不支持,请上传 .xml文件');
                }

                $moveDir = ROOT_PATH . "/gamexml/";
                $path = $moveDir . $name;
                if(!MooFile::isExists($moveDir)) {
                    MooFile::mkDir($moveDir);
                }
                $moveRs = move_uploaded_file($tmp_name, $path);

                if(!$moveRs) {
                    MooView::set('fileUploadCode', -1);
                    MooView::set('fileUploadMessage', '文件移动失败,请重试!');
                }else{
                    $userName = MooObj::get('User')->getUserName();
                    if(!$userName) {
                        MooView::set('fileUploadCode', -1);
                        MooView::set('fileUploadMessage', '请先登录');
                    }else{
                        MooView::set('fileUploadCode', 0);
                        MooView::set('fileUploadMessage', '上传成功');
                    }
                }

            }

        }else{
            MooView::set('fileUploadCode', 0);
            MooView::set('fileUploadMessage', '');
        }
        /*遍历gamexml文件夹*/
        $configDir = ROOT_PATH . "/gamexml/";
        $configFiles = array();
        if(@$handle = opendir($configDir)) { //注意这里要加一个@，不然会有warning错误提示：）
            while(($file = readdir($handle)) !== false) {
                if($file != ".." && $file != ".") { //排除根目录
                    $houzhui = substr(strrchr($file, '.'), 1);
                    $basename = basename($file,".".$houzhui);
                    $configFiles[] = array('basename'=>$basename,'fileName'=>$file);
                }
            }
            closedir($handle);
        }
        $url = MooConfig::get('main.url.serversforstate');
        $res = MooUtil::curl_send($url, null, 1);
        $servers = MooJson::decode($res, true);
        if($servers){
            MooView::set('servers', $servers['servers']);
        }

        MooView::set('configFiles', $configFiles);
        MooView::render('serverConfig');
    }

    /**
     * 获取上传文件类型
     * @param unknown $type
     * @return string
     */
    private function getFileExt($type) {
        switch ($type) {
            case "application/vnd.ms-excel" :
                $extType = "excel";
                break;
            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :
                $extType = "excel";
                break;
            case "text/xml" :
                $extType = "xml";
                break;
            default :
                $extType = "type_error";
                break;
        }
        return $extType;
    }
}